package cn.iocoder.yudao.module.system.controller.admin.record;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.system.controller.admin.nodeconfig.vo.NodeConfigSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.record.ApprovalRecordDO;
import cn.iocoder.yudao.module.system.service.record.ApprovalRecordService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.record.vo.*;

@Tag(name = "管理后台 - 审核操作记录")
@RestController
@RequestMapping("/systemRecord")
@Validated
public class ApprovalRecordController {

    @Resource
    private ApprovalRecordService approvalRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建审核操作记录")
    public CommonResult<Integer> createRecord(@Valid @RequestBody ApprovalRecordSaveReqVO createReqVO) {
        return success(approvalRecordService.createRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审核操作记录")
    public CommonResult<Boolean> updateRecord(@Valid @RequestBody ApprovalRecordSaveReqVO updateReqVO) {
        approvalRecordService.updateRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审核操作记录")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteRecord(@RequestParam("id") Integer id) {
        approvalRecordService.deleteRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除审核操作记录")
    public CommonResult<Boolean> deleteRecordList(@RequestParam("ids") List<Integer> ids) {
        approvalRecordService.deleteRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得审核操作记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ApprovalRecordRespVO> getRecord(@RequestParam("id") Integer id) {
        ApprovalRecordDO record = approvalRecordService.getRecord(id);
        return success(BeanUtils.toBean(record, ApprovalRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得审核操作记录分页")
    public CommonResult<PageResult<ApprovalRecordRespVO>> getRecordPage(@Valid ApprovalRecordPageReqVO pageReqVO) {
        PageResult<ApprovalRecordDO> pageResult = approvalRecordService.getRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApprovalRecordRespVO.class));
    }

    /**
     * 审核
     */
    @PostMapping("/portal/audit")
    @Operation(summary = "审核")
    public CommonResult<Boolean> audit(@Valid @RequestBody ApprovalRecordSaveReqVO auditReqVO) {
        try {
            Boolean result = approvalRecordService.auditApplication(auditReqVO);;
            return success(result);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取审核记录
     * @param pageReqVO
     * @throws IOException
     */
    @GetMapping("/portal/getAuditRecord")
    @Operation(summary = "获取最近一条审核不通过的操作记录")
    public CommonResult<ApprovalRecordDO> getAuditRecord(@Valid ApprovalRecordPageReqVO pageReqVO){
        return success(approvalRecordService.getAuditRecord(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出审核操作记录 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecordExcel(@Valid ApprovalRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ApprovalRecordDO> list = approvalRecordService.getRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "审核操作记录.xls", "数据", ApprovalRecordRespVO.class,
                        BeanUtils.toBean(list,ApprovalRecordRespVO.class));
    }

}